<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

if ($user->getUserId() != 1)
	goToPage("/admin/users");

$errors = array();

if ($route->POST('action') == 'add')
{
	$login = $route->POST('login');
	$password = $route->POST('password');
	$re_password = $route->POST('re_password');
	$salt = rand(1000,9999);
	$type = $route->POST('type');
	$last_time = time();
	$last_ip = $_SERVER['REMOTE_ADDR'];
	$company = $route->POST("company");
	
	$a_user = $db->getCell("select `login` from `users` where `login` = '$login' limit 1");
	if ($a_user != null)
		$errors[] = 'Уже существует пользователь с таким логином!';
	
	if ($password != $re_password)
		$errors[] = "Повтор пароля не совпадает с паролем";
	
	if (count($errors) == 0)
	{
		$password = md5(md5($password).$salt);
		$query = "insert into `users` values(NULL,'$login','$password','$salt','$type','$last_time','$last_ip')";
		
		if ($db->sendQuery($query)){
			$a_user_id = mysql_insert_id();
			if ($type == "agent"){
				$db->sendQuery("insert into `agent` values (NULL,'$a_user_id','$company')");
			}
			goToPage("/admin/users/edit/".$a_user_id);
		}
	}
}

loadPage("head.php");
?>
<script type="text/javascript">
$(document).ready(function(){
	$("#type").change(function(){
		if ($(this).val() == "agent"){
			$("#agent_company").show();
		} else {
			$("#agent_company").hide();
		}
	})
})
</script>
<h1>Добавление пользователя</h1><br /><?php
	if (count($errors) > 0)
		echo "<p class='fail'>".implode("<br />",$errors)."<br /><br /></p>";
?>
<form method='POST' id='edit'>
	<input type='hidden' name='action' value='add' />
	<table cellpadding='3' cellspacing='5'>
		<tr>
			<td width='170px'>Логин</td>
			<td width='300px'><input type='text' name='login' value='<?php echo $route->POST("login"); ?>' /></td>
		</tr>
		<tr>
			<td>Пароль</td>
			<td><input type='password' name='password' /></td>
		</tr>
		<tr>
			<td>Повтор пароля</td>
			<td><input type='password' name='re_password' /></td>
		</tr>
		<tr>
			<td>Тип</td>
			<td>
				<select name="type" id="type" class="double_short">
					<option value="agent">Агент</option>
					<option value="manager"<?php if($route->POST("type") == "manager") echo " selected='selected'"; ?>>Менеджер</option>
					<option value="administrator"<?php if($route->POST("type") == "administrator") echo " selected='selected'"; ?>>Администратор</option>
				</select>
			</td>
		</tr>
		<tr id="agent_company" <?php if ($route->POST("type") == "manager" || $route->POST("type") == "administrator") echo "style='display: none'"; ?>>
			<td>Компания агента</td>
			<td>
				<select name="company" class="double_short">
					<?php
					$companies = $db->getTable("select `company_id`,`name` from `companies` where `head_office` = '0' order by `name`");
					echo select_create($companies,"company_id","name",$route->POST("company"));
					?>
				</select>
			</td>
		</tr>
		<tr>
			<td colspan='2' align='right'><input type='submit' value='Добавить' /></td>
		</tr>
	</table>
</form>

<?php loadPage("footer.php"); ?>